package com.xinyi.traditionmvvm.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.xinyi.basic.utils.LogUtil;
import com.xinyi.traditionmvvm.app.RetrofitClient;
import com.xinyi.traditionmvvm.model.NeteaseCommentModel;
import com.xinyi.traditionmvvm.model.Resource;
import com.xinyi.traditionmvvm.model.ResponseData;
import com.xinyi.traditionmvvm.sources.local.LocalApi;
import com.xinyi.traditionmvvm.sources.local.impl.LocalApiImpl;
import com.xinyi.traditionmvvm.sources.remote.NeteaseApi;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author 杨耿雷
 * @date 2024/9/26 10:00
 * @description 主页ViewModel
 */
public class MainViewModel extends ViewModel {

    /**
     * 网易云音乐API
     */
    private final NeteaseApi mNeteaseApi;

    /**
     * 网抑云音乐热评数据
     */
    private final MutableLiveData<Resource<NeteaseCommentModel>> mNeteaseComment;

    public MutableLiveData<Resource<NeteaseCommentModel>> getNeteaseComment() {
        return mNeteaseComment;
    }

    /**
     * 本地数据API
     */
    private final LocalApi mLocalApi;

    /**
     * 本地数据
     */
    private final MutableLiveData<Resource<String>> mLocalData;

    public MutableLiveData<Resource<String>> getLocalData() {
        return mLocalData;
    }

    public MainViewModel() {
        mNeteaseApi = RetrofitClient.getRetrofitInstance().create(NeteaseApi.class);
        mLocalApi = new LocalApiImpl();
        mNeteaseComment = new MutableLiveData<>();
        mLocalData = new MutableLiveData<>();
    }

    /**
     * 随机获取一条网抑云音乐热评
     * @param format 接口数据格式 text/json
     */
    public void requestNeteaseComment(String format) {
        mNeteaseApi.getNeteaseComment(format)
                .subscribeOn(Schedulers.io()) // 在 IO 线程中请求
                .observeOn(AndroidSchedulers.mainThread()) // 在主线程中观察结果
                // 订阅观察者
                .subscribe(new Observer<ResponseData<NeteaseCommentModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        // 可以在这里做一些初始化操作
                        mNeteaseComment.setValue(new Resource.Loading<>());
                    }

                    @Override
                    public void onNext(ResponseData<NeteaseCommentModel> responseData) {
                        // 请求成功
                        LogUtil.d("请求成功：" + responseData.getData().getContent());
                        mNeteaseComment.setValue(new Resource.Success<>(responseData.getData()));
                    }

                    @Override
                    public void onError(Throwable e) {
                        // 请求失败
                        mNeteaseComment.setValue(new Resource.Fail<>(e));
                    }

                    @Override
                    public void onComplete() {
                        // 请求完成
                        // TODO 如有需求可以在Resource里扩展新的数据状态
                    }
                });
    }

    /**
     * 请求本地数据
     */
    public void requestLocalApi() {
        mLocalApi.getLocalData().subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mLocalData.setValue(new Resource.Loading<>());
            }

            @Override
            public void onNext(String s) {
                mLocalData.setValue(new Resource.Success<>(s));
            }

            @Override
            public void onError(Throwable e) {
                mLocalData.setValue(new Resource.Fail<>(e));
            }

            @Override
            public void onComplete() {  }
        });
    }
}
